<?php
class Page{
	//总共多少条
	private $count;
	//每页多少条
	private $row;
	//总页数
	private $pageNum;
	//当前页
	private $selfPage;

	//获取当前URL
	private $url;

	public function __construct($count, $row)
	{
		$this->count=$count;
		$this->row=$row;
		$this->url=$this->getUrl();
		//进一法 求总页数
		$this->pageNum=ceil($this->count/$this->row);
		$this->selfPage=isset($_GET['p'])?intval($_GET['p']):1;
	}

	public function getUrl()
	{
		$get=$_GET;
		if(isset($get['p'])){
			unset($get['p']);
		};
		$url=__WEB__.'?';
		foreach ($get as $k => $v) {
			$url.=$k."=$v&";
		}
		return $url.'p=';
	}

	//上一页
	public function prePage()
	{
		if ($this->selfPage>1) {
			return "<a href='".$this->url.($this->selfPage-1)."'>上一页</a>";
		}else{
			return "<span class='pre'>上一页</span>";
		}
	}

	//下一页
	public function nextPage()
	{
		if ($this->selfPage<$this->pageNum) {
			return "<a href='".$this->url.($this->selfPage+1)."'>下一页</a>";
		}else{
			return "<span class='next'>下一页</span>";
		}
	}
	public function pageList()
	{
		$str='';
		for ($i=1; $i <= $this->pageNum ; $i++) { 
			if ($i==$this->selfPage) {
				$str.="<span class='current'>$i</span>";
			}else{
				$str.="<a href='".$this->url.$i."' >$i</a>";
			}
		}
			return $str;
	}

	public function show()
	{
		return $this->prePage().$this->pageList().$this->nextPage();
	}
	public function limit()
	{
		return ($this->selfPage-1)*$this->row.','.$this->row;
	}
}